local hx__bingjiyuan = fk.CreateSkill{
    name = "hx__bingjiyuan",

    tags = {Skill.Limited},
}

hx__bingjiyuan:addEffect(fk.EnterDying,{
    can_trigger = function (self, event, target, player, data)
        return player:hasSkill(hx__bingjiyuan.name) and player:usedSkillTimes(hx__bingjiyuan.name,Player.HistoryGame) == 0 and target and target == player
    end,
    on_cost = function (self, event, target, player, data)
        return player.room:askToSkillInvoke(player,{ skill_name = hx__bingjiyuan.name , prompt = "#hx__bingjiyuan__evoke"})
    end,
    on_use = function (self, event, target, player, data)
        local room = player.room
        if player.general == "hxjsct__qingzhi__kuzan" then
            room:setPlayerProperty(player,"general","hxjsct__qingzhi__kuzan__juexing")
        elseif player.deputyGeneral == "hxjsct__qingzhi__kuzan" then  
            room:setPlayerProperty(player,"deputyGeneral","hxjsct__qingzhi__kuzan__juexing")
        end
        room:recover{
            who = player,
            num = 4,
            recoverBy = player,
            skillName = hx__bingjiyuan.name
        }
        local tars = room:askToChoosePlayers(player,{targets = room.alive_players, min_num = 1, max_num = 999, skill_name = hx__bingjiyuan.name, cancelable = true, prompt = "#hx__bingjiyuan__bingchoose", no_indicate = false})
        if #tars > 0 then
            for _, v in ipairs(tars) do
                room:setPlayerMark(v,"@hx__bingjuexing__bingdong",1)
            end
        end
        player:gainAnExtraTurn(false,hx__bingjiyuan.name)
    end,
})

hx__bingjiyuan:addEffect(fk.AfterSkillEffect,{
    can_trigger = function (self, event, target, player, data)
        return player:hasSkill(hx__bingjiyuan.name) and player:usedSkillTimes(hx__bingjiyuan.name,Player.HistoryGame) == 0 and target and target == player
        and data.skill and data.skill.name == "hx__bingjuexing"
    end,
    on_cost = function (self, event, target, player, data)
        player.room:addPlayerMark(player,"hx__bingjuexing__record",1)
        if player:getMark("hx__bingjuexing__record") == 12 then
            return player.room:askToSkillInvoke(player,{ skill_name = hx__bingjiyuan.name , prompt = "#hx__bingjiyuan__evoke"})
        end
    end,
    on_use = function (self, event, target, player, data)
        local room = player.room
        if player.general == "hxjsct__qingzhi__kuzan" then
            room:setPlayerProperty(player,"general","hxjsct__qingzhi__kuzan__juexing")
        elseif player.deputyGeneral == "hxjsct__qingzhi__kuzan" then  
            room:setPlayerProperty(player,"deputyGeneral","hxjsct__qingzhi__kuzan__juexing")
        end
        room:recover{
            who = player,
            num = 4,
            recoverBy = player,
            skillName = hx__bingjiyuan.name
        }
        local tars = room:askToChoosePlayers(player,{targets = room.alive_players, min_num = 1, max_num = 999, skill_name = hx__bingjiyuan.name, cancelable = true, prompt = "#hx__bingjiyuan__bingchoose", no_indicate = false})
        if #tars > 0 then
            for _, v in ipairs(tars) do
                room:setPlayerMark(v,"@hx__bingjuexing__bingdong",1)
            end
        end
        player:gainAnExtraTurn(false,hx__bingjiyuan.name)
    end,
})


hx__bingjiyuan:addEffect(fk.TurnEnd,{
    can_refresh = function (self, event, target, player, data)
        return player:hasSkill(hx__bingjiyuan.name) and target and target == player and not player:insideExtraTurn()
    end,
    on_refresh = function (self, event, target, player, data)
        local room = player.room
        local targets = table.filter(room.alive_players,function (p)
            return p:getMark("@hx__bingjuexing__bingdong") > 0
        end)
        if #targets > 0 then
            for _, v in ipairs(targets) do
                room:setPlayerMark(v,"@hx__bingjuexing__bingdong",0)
            end
        end
    end,
})

hx__bingjiyuan:addEffect("prohibit",{
    prohibit_use = function (self, player, card)
        return player:getMark("@hx__bingjuexing__bingdong") > 0
    end,
})

return hx__bingjiyuan